package com.djdg.foodies.admin.base.interceptor;

import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 响应请求com.djdg.foodies.admin.controller.*的日志
 * @Auther Demon
 * @Date 2017/12/15 17:14 星期五
 */
@Aspect
@Component
@Order(10)
public class ResponseLogsInterceptor {

    private static final Logger logger = LoggerFactory.getLogger(ResponseLogsInterceptor.class);

    /**
     * 定义拦截规则：拦截com.vistask.api.controller包下面的所有类中
     */
    @Pointcut("execution(* com.djdg.foodies.admin.controller..*(..)) ")
    public void controllerMethodPointcut(){
    }


    @AfterReturning(returning = "ret", pointcut = "controllerMethodPointcut()")
    public void doAfterReturning(Object ret){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // 处理完请求，返回内容
        logger.info("**************************Response Info**************************");
        String arg = JSONObject.toJSONString(ret);
        logger.info("Request From : {}  {}", request.getMethod(), request.getRequestURL());
        logger.info("Response : {} " , arg);
    }

}
